package pers.leeyns.rd.en;

import pers.leeyns.rd.base.RandomStringGenerator;
import pers.leeyns.rd.entity.EnStringModel;

/**
 * @description 随机英文生成器
 * @author Leeyns
 * @date 2016年2月16日
 */
public class RandomEnglishGenerator extends RandomStringGenerator{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2294527039268840144L;
	private static final String EN_MODEL_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private static final String EN_MODEL_LOWER = "abcdefghijklmnopqrstuvwxyz";
	private static final String EN_MODEL_UPPER_LOWER = EN_MODEL_UPPER+EN_MODEL_LOWER;
	public static synchronized  RandomEnglishGenerator build(){
		return new RandomEnglishGenerator();
	}
	
	/**
	 * 获取单个英文字母
	 * @return
	 */
	public String getRandomEnString(EnStringModel enStringModel){
		if(EnStringModel.EN_UPPER == enStringModel){
			return String.valueOf(EN_MODEL_UPPER.charAt(this.nextInt(EN_MODEL_UPPER.length()))); 
		}else if(EnStringModel.EN_LOWER == enStringModel){
			return String.valueOf(EN_MODEL_LOWER.charAt(this.nextInt(EN_MODEL_LOWER.length())));
		}else{
			return String.valueOf(EN_MODEL_UPPER_LOWER.charAt(this.nextInt(EN_MODEL_UPPER_LOWER.length())));
		}
	}
	
	/**
	 * 随机获取多个英文字母
	 * @param length 字母个数
	 * @return
	 */
	public String getRandomEnString(int length,EnStringModel enStringModel){
		StringBuffer sbf = new StringBuffer();
		for(int i=0 ;i< length;i++){
			sbf.append(getRandomEnString(enStringModel));
		}
		return sbf.toString();
	}
	 
	
	/**
	 * 在 res 英文字符串中随机获取 length 个字符
	 * @param res 源字符串
	 * @param length 字符串长度
	 * @return
	 */
	public String getRandomEnString(String res,int length){
		if(!res.matches("[a-zA-Z]+")){
			throw new IllegalArgumentException("Argument 'res' is not Englist String:"+res);
		}
		return this.getRandomString(res, length);
	}
	
	
	public static void main(String[] args) {
//		while (true){
//			System.out.println(RandomEnglishGenerator.build().getRandomEnString(10,EnStringModel.EN_UPPER_LOWER));
//		}
	}
}
